package com.pudu.mydemo.entity;

import android.app.Activity;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;

import com.pudutech.pdrobot.R;

import java.io.Serializable;

import static android.util.Log.e;

/**
 * Created by guiliu on 2016/7/23.
 */
public class AccessPoint implements Comparable<AccessPoint>,Serializable{
    private String Tag = this.getClass().getSimpleName();
    private int mRssi;
    private String ssid;
    private String bssid;
    private ScanResult scanResult;
    private NetworkInfo.DetailedState mState;
    private int networkId;
    private WifiInfo mInfo;
    private WifiConfiguration mConfig;
    /** These values are matched in string arrays -- changes must be kept in sync */
    static final int SECURITY_NONE = 0;
    static final int SECURITY_WEP = 1;
    static final int SECURITY_PSK = 2;
    static final int SECURITY_EAP = 3;

    int security;

    public AccessPoint(WifiConfiguration config) {
        loadConfig(config);

    }

    public AccessPoint(ScanResult result) {
        loadResult(result);
    }

    public AccessPoint(ScanResult scanResult, int mNetworkId, NetworkInfo.DetailedState detailedState) {
        this.scanResult = scanResult;
        this.networkId = mNetworkId;
        this.mState = detailedState;
        security = getSecurity(scanResult);
        mRssi = scanResult.level;
    }

    private void loadConfig(WifiConfiguration config) {
        ssid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID));
        bssid = config.BSSID;
        security = getSecurity(config);
        networkId = config.networkId;
        mRssi = Integer.MAX_VALUE;
        mConfig = config;
    }

    private void loadResult(ScanResult result) {
        ssid = result.SSID;
        bssid = result.BSSID;
        networkId = -1;
        mRssi = result.level;
        security = getSecurity(result);
        scanResult = result;
    }
    public ScanResult getScanResult() {
        return scanResult;
    }

    public NetworkInfo.DetailedState getState() {
        return mState;
    }


    static int getSecurity(WifiConfiguration config) {
        if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
            return SECURITY_PSK;
        }
        if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP) ||
                config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
            return SECURITY_EAP;
        }
        return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;
    }

    private static int getSecurity(ScanResult result) {
        if (result.capabilities.contains("WEP")) {
            return SECURITY_WEP;
        } else if (result.capabilities.contains("PSK")) {
            return SECURITY_PSK;
        } else if (result.capabilities.contains("EAP")) {
            return SECURITY_EAP;
        }
        return SECURITY_NONE;
    }

    @Override
    public int compareTo(AccessPoint other) {
        if (mInfo != other.mInfo) {
            return (mInfo != null) ? -1 : 1;
        }
        // Reachable one goes before unreachable one.
        if ((mRssi ^ other.mRssi) < 0) {
            return (mRssi != Integer.MAX_VALUE) ? -1 : 1;
        }
        // Configured one goes before unconfigured one.
        if ((networkId ^ other.networkId) < 0) {
            return (networkId != -1) ? -1 : 1;
        }
        // Sort by signal strength.
        int difference = WifiManager.compareSignalLevel(other.mRssi, mRssi);
        if (difference != 0) {
            return difference;
        }
        // Sort by ssid.
        return ssid.compareToIgnoreCase(other.ssid);
    }


   public void update(WifiInfo info, NetworkInfo.DetailedState state) {
        boolean reorder = false;
         Log.e(Tag,"info"+info);
//        Log.e(Tag,"state"+state);
       Log.e(Tag,"ssid:"+ssid+" networkID:"+networkId);
        if (info != null && networkId != -1
                && networkId == info.getNetworkId()) {
            reorder = (mInfo == null);
            mRssi = info.getRssi();
            mInfo = info;
            mState = state;
//            refresh();
        } else if (mInfo != null) {
            reorder = true;
            mInfo = null;
            mState = null;


        }

        if (reorder) {

        }
    }
    public int getLevel() {
        if (mRssi == Integer.MAX_VALUE) {
            return -1;
        }
        return WifiManager.calculateSignalLevel(mRssi, 4);
    }

    public int getSecurity() {
        return security;
    }

    public boolean update(ScanResult result) {
        if (ssid.equals(result.SSID)/* && security == getSecurity(result)*/) {
            if (WifiManager.compareSignalLevel(result.level, mRssi) > 0) {
                int oldLevel = getLevel();
                mRssi = result.level;
//                if (getLevel() != oldLevel) {
//                    notifyChanged();
//                }
            }
            // This flag only comes from scans, is not easily saved in config
//            if (security == SECURITY_PSK) {
//                pskType = getPskType(result);
//            }
//            refresh();
            return true;
        }
        return false;
    }
    public WifiInfo getInfo() {
        return mInfo;
    }
    static String removeDoubleQuotes(String string) {
        int length = string.length();
        if ((length > 1) && (string.charAt(0) == '"')
                && (string.charAt(length - 1) == '"')) {
            return string.substring(1, length - 1);
        }
        return string;
    }

    static String convertToQuotedString(String string) {
        return "\"" + string + "\"";
    }

    public int getmRssi() {
        return mRssi;
    }

    public void setmRssi(int mRssi) {
        this.mRssi = mRssi;
    }

    public String getSsid() {
        return ssid;
    }

    public void setSsid(String ssid) {
        this.ssid = ssid;
    }

    public String getBssid() {
        return bssid;
    }

    public void setBssid(String bssid) {
        this.bssid = bssid;
    }

    public NetworkInfo.DetailedState getmState() {
        return mState;
    }

    public void setmState(NetworkInfo.DetailedState mState) {
        this.mState = mState;
    }

    public int getNetworkId() {
        return networkId;
    }

    public void setNetworkId(int networkId) {
        this.networkId = networkId;
    }

    public WifiInfo getmInfo() {
        return mInfo;
    }

    public void setmInfo(WifiInfo mInfo) {
        this.mInfo = mInfo;
    }

    public WifiConfiguration getmConfig() {
        return mConfig;
    }

    public void setmConfig(WifiConfiguration mConfig) {
        this.mConfig = mConfig;
    }
}
